package com.book.lizhi.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.book.lizhi.R;
import com.book.lizhi.http.OnHttpListener;
import com.book.lizhi.util.AppLog;

public class MoreListView extends ListView implements OnHttpListener {
	private View mFootView;
	private ProgressBar mProgressBar;
	private TextView mTipView;
	private OnMoreListener moreListener;
	private boolean isComplete;
	private boolean isLoading = false;

	public interface OnMoreListener {
		public void onLoadMore();

		public void onMore(boolean isCache,String url, String response);
	}

	public MoreListView(Context context) {
		super(context);
		initView(context);
	}

	public MoreListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		mFootView = View.inflate(context, R.layout.more_footview_layout, null);
		mProgressBar = (ProgressBar) mFootView.findViewById(R.id.more_progressBar);
		// mProgressBar.setVisibility(View.GONE);
		mTipView = (TextView) mFootView.findViewById(R.id.more_tip_view);
		addFooterView(mFootView, null, true);
		mFootView.setVisibility(View.GONE);
		setOnScrollListener(new OnScrollListener() {

			private int lastVisibleIndex;

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				AppLog.e("lastVisibleIndex:"+lastVisibleIndex);
				AppLog.e("size:"+getAdapter().getCount());
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && lastVisibleIndex == getAdapter().getCount()) {
					// 当滑到底部时自动加载
					if (moreListener != null && !isLoading && !isComplete) {
						mFootView.setVisibility(View.VISIBLE);
						moreListener.onLoadMore();
					}
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				lastVisibleIndex = firstVisibleItem + visibleItemCount;
			}
		});
	}

	@Deprecated
	@Override
	public void onResponse(String response) {
	}

	@Override
	public void onErrorResponse(VolleyError error) {
		isLoading = false;
		mProgressBar.setVisibility(View.GONE);
		mTipView.setText("网络错误请稍后再试!");
	}

	@Override
	public void onHttpStart() {
		isLoading = true;
		mProgressBar.setVisibility(View.VISIBLE);
		mTipView.setText("加载中...");
		mFootView.setVisibility(View.VISIBLE);
	}

	public OnMoreListener getMoreListener() {
		return moreListener;
	}

	public void setMoreListener(OnMoreListener moreListener) {
		this.moreListener = moreListener;
	}

	public boolean isComplete() {
		return isComplete;
	}

	public void setComplete(boolean isComplete) {
		this.isComplete = isComplete;
		if (isComplete) {
			//mFootView.setVisibility(View.VISIBLE);
			mTipView.setText("没有更多了!");
			mProgressBar.setVisibility(View.GONE);
		}
	}
	
	public void reset(){
		this.isComplete=false;
		mTipView.setText("上拉加载更多");
		mProgressBar.setVisibility(View.VISIBLE);
		mFootView.setVisibility(View.GONE);
	}

	public boolean isLoading() {
		return isLoading;
	}

	@Override
	public void onResponse(boolean isCache,String url, String json) {
		isLoading = false;
		// mProgressBar.setVisibility(View.GONE);
		// mTipView.setText("点击加载更多");
		//mFootView.setVisibility(View.GONE);
		if (moreListener != null) {
			moreListener.onMore(isCache,url, json);
		}
	}

}
